package com.pp.admin.main.client.tab;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.pp.admin.common.client.beans.User;
import com.pp.admin.common.client.helper.ActionHandler;
import com.pp.admin.main.client.common.TabListContents;

/**
 * @author pandurang
 */
public class UserPage extends BasePage {

	private static int TAB_CONTENTS_INDEX = 2;

	class CreateHandler implements ClickHandler {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
		 * .event.dom.client. ClickEvent)
		 */
		@Override
		public void onClick(ClickEvent event) {

			UserPage.this.setTabContents(new CreateUserPanel(UserPage.this));
		}
	}

	class CancelHandler implements ClickHandler {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
		 * .event.dom.client. ClickEvent)
		 */
		@Override
		public void onClick(ClickEvent event) {

			ListUserPanel listPanel = new ListUserPanel(UserPage.this);
			listPanel.refresh();
			UserPage.this.setTabContents(listPanel);

		}
	}

	class SaveClickHandler implements ClickHandler {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
		 * .event.dom.client. ClickEvent)
		 */
		@Override
		public void onClick(ClickEvent event) {

			CreateUserPanel createUserPanel = null;
			if (UserPage.this.tabContents instanceof CreateUserPanel) {
				createUserPanel = (CreateUserPanel) UserPage.this.tabContents;
			}

			createUserPanel.validate();
			User user = createUserPanel.getUser();
//			user.setStatus(User.STATUS_DELETED);
			UserPage.userService.save(user, new AsyncCallback<Void>() {

				@Override
				public void onFailure(Throwable caught) {

					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(Void result) {

					UserPage.this.refresh();
				}

			});
		}
	}

	class UpdateClickHandler implements ClickHandler {
		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt
		 * .event.dom.client. ClickEvent)
		 */
		@Override
		public void onClick(ClickEvent event) {
			/**
			 * Add code to check dirty flag. If it is not dirty don't have to
			 * update.
			 */
			CreateUserPanel createUserPanel = null;
			if (UserPage.this.tabContents instanceof CreateUserPanel) {
				createUserPanel = (CreateUserPanel) UserPage.this.tabContents;
			}

			createUserPanel.validate();
			User user = createUserPanel.getUser();
//			user.setStatus(User.STATUS_DELETED);
			UserPage.userService.update(user, new AsyncCallback<Void>() {

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onSuccess(Void result) {
					UserPage.this.refresh();

				}
			});
		}

	}

	class EditClickHandler implements ActionHandler {

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * com.pp.admin.common.client.helper.ActionHandler#action(java.lang.
		 * String)
		 */
		@Override
		public void action(String objectID) {

			UserPage.userService.getUserByID(objectID,
					new AsyncCallback<User>() {

						@Override
						public void onFailure(Throwable caught) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(User result) {
							UserPage.this.setTabContents(new CreateUserPanel(
									UserPage.this, result));
						}
					});

		}

	}

	public UserPage(BaseTab parent) {

		super();
		this.parentTab = parent;
		this.setTabContents(new ListUserPanel(this));
	}

	private void setTabContents(VerticalPanel contents) {

		this.tabContents = contents;
		cleanContents();
		this.add(contents);
	}

	private void cleanContents() {

		if (getWidgetCount() == 3) {
			remove(TAB_CONTENTS_INDEX);
		}

	}

	@Override
	public void refresh() {

		if (!(this.tabContents instanceof TabListContents)) {
			setTabContents(new ListUserPanel(this));
		}
		((TabListContents) this.tabContents).refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * 
	 * @seecom.google.gwt.event.dom.client.ChangeHandler#onChange(com.google.gwt.
	 * event.dom.client. ChangeEvent)
	 */
	@Override
	public void onChange(ChangeEvent event) {

		refresh();
	}
}
